package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbeanTwo;
import com.itboye.eweilisten.util.widget.AutoResizeTextView;
import com.itboye.eweilisten.util.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 情景对话
 */
public class DuiHuaAdapter extends RecyclerView.Adapter<DuiHuaAdapter.MyViewHolder> {

    Context context;
    private List<TIbeanTwo> beans;

    public DuiHuaAdapter(Context context, List<TIbeanTwo> beans) {
        this.context = context;
        this.beans = beans;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_duihua, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.timu_num.setText(position + 1 + "");
        int i = position;
        for (int j = 0; j < beans.get(position).getContent().size(); j++) {
            questionContentAdd(holder.questionLine, beans.get(position).getContent().get(j), holder.answerLine);
        }
        for (int j = 0; j < beans.get(i).getAnswers().size(); j++) {
            answerLineContentAdd(holder.answerLine, beans.get(i).getOption().get(j).getOptions(), holder.questionLine, position);
        }
        for (int j = 0; j < holder.questionLine.getChildCount(); j++) {
            if ("null".equals(holder.questionLine.getChildAt(j).getTag(R.id.selected) + "")) {

            } else {
                if (0 == Integer.parseInt(holder.questionLine.getChildAt(j).getTag(R.id.selected) + "")) {
                    // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                    holder.questionLine.getChildAt(j).setBackgroundColor(Color.parseColor("#3b7830"));
                    holder.questionLine.getChildAt(j).setTag(R.id.selected, 2);
                    break;
                }
            }
        }
    }

    /**
     * 内容
     *
     * @param questionLine
     * @param string
     * @param answerLine
     */
    private void questionContentAdd(final FlowLayout questionLine, ArrayList<String> string, final FlowLayout answerLine) {

        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.setMargins(0, 0, 25, 0);
        params.setMargins(0, 5, 0, 5);
        for (int i = 0; i < string.size(); i++) {
            if ("_".equals(string.get(i))) {
                AutoResizeTextView textView = new AutoResizeTextView(context);
                textView.setText(string.get(i) + "__");
                textView.setTextSize(16);
                textView.setFocusable(false);
                textView.setTextColor(Color.parseColor("#3b7830"));
                textView.setTag(R.id.selected, 0);
                textView.setPadding(15, 0, 15, 0);
                textView.setLayoutParams(params);
                questionLine.addView(textView);

                //点击事件
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        // 这个要分两个方法去做
                        if ("___".equals(((TextView) v).getText() + "")) {
                            // 无填好的内容
                            for (int i = 0; i < questionLine.getChildCount(); i++) {

                                // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                                if ("2".equals(questionLine.getChildAt(i).getTag(R.id.selected) + "")) {
                                    questionLine.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff" + ""));
                                    questionLine.getChildAt(i).setTag(R.id.selected, 0);
                                    break;
                                }
                            }
                            ((TextView) v).setBackgroundColor(Color.parseColor("#3b7830"));
                            // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                            ((TextView) v).setTag(R.id.selected, 2);

                        } else {
                            // 有填好的内容
                            for (int i = 0; i < questionLine.getChildCount(); i++) {

                                // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                                if ("2".equals(questionLine.getChildAt(i).getTag(R.id.selected) + "")) {
                                    questionLine.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff" + ""));
                                    questionLine.getChildAt(i).setTag(R.id.selected, 0);
                                }
                            }
                            for (int i = 0; i < answerLine.getChildCount(); i++) {
                                if (((TextView) v).getText().toString()
                                        .equals(((TextView) answerLine.getChildAt(i)).getText())) {
                                    ((TextView) answerLine.getChildAt(i))
                                            .setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
                                    // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                                    ((TextView) answerLine.getChildAt(i)).setTag(R.id.selected, 1);
                                    ((TextView) answerLine.getChildAt(i)).setEnabled(true);
                                    ((TextView) answerLine.getChildAt(i)).setClickable(true);
                                }
                            }
                            ((TextView) v).setText("___");
                            ((TextView) v).setBackgroundColor(Color.parseColor("#3b7830"));
                            // tag标记位 0 无数据未选中 1有数据未选-中 2无数据选中 3有数据选中
                            ((TextView) v).setTag(R.id.selected, 2);
                        }
                    }
                });


            } else {
                TextView textView = new TextView(context);
                if (i == 0) {
                    textView.setText(string.get(i) + ":");
                } else {
                    textView.setText(string.get(i));
                }
                textView.setTextSize(16);
                textView.setTextColor(Color.parseColor("#3b7830"));
                textView.setLayoutParams(params);
                questionLine.addView(textView);
            }
        }
    }

    /**
     * 答案
     *
     * @param answerLine
     * @param string
     * @param questionLine
     */
    private void answerLineContentAdd(FlowLayout answerLine, String string, final FlowLayout questionLine, final int position) {
        // 设置添加的textvIew的参数
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 25, 5);

        TextView textView = new TextView(context);
        textView.setText(string);
        textView.setTextSize(16);
        textView.setTextColor(Color.parseColor("#ffffff"));
        // textView.setBackgroundColor(Color.parseColor("#0000ff"));

        textView.setLayoutParams(params);
        textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
        answerLine.addView(textView);

        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (int i = 0; i < questionLine.getChildCount(); i++) {
                    // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                    if ("2".equals(questionLine.getChildAt(i).getTag(R.id.selected) + "")) {
                        String sss = ((TextView) v).getText().toString();
                        ((TextView) questionLine.getChildAt(i)).setText(Html.fromHtml("<u>" + sss + "</u>"));

                        //点击的时候遍历出答案是否正确
                        StringBuffer sb = new StringBuffer();
                        for (int ii = 0; ii < questionLine.getChildCount(); ii++) {
                            if (questionLine.getChildAt(ii) instanceof AutoResizeTextView) {
                                String to = ((TextView) questionLine.getChildAt(ii)).getText().toString();
                                sb.append(to);
                            }
                        }
                        StringBuilder ssss = new StringBuilder();
                        for (int j = 0; j < beans.get(position).getAnwser_true().size(); j++) {
                            ssss.append(beans.get(position).getAnwser_true().get(j));
                        }
                        System.out.println("UIIHIUHHOIS" + sb);
                        System.out.println("UIIHIUHHOIS" + ssss);
                        if (sb.equals("")) {
                            beans.get(position).setXuan(2);//2代表没有输入
                        } else if (sb.toString().equals(ssss.toString())) {
                            beans.get(position).setXuan(0);//0代表选择正确
                        } else {
                            beans.get(position).setXuan(1);//0代表选择错误
                        }

                        v.setEnabled(false);
                        v.setClickable(false);
                        ((TextView) v).setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_04));
                        // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                        questionLine.getChildAt(i).setTag(R.id.selected, 1);
                        for (int j = 0; j < questionLine.getChildCount(); j++) {
                            questionLine.getChildAt(j).setBackgroundColor(Color.parseColor("#ffffff"));
                        }

                        for (int j = 0; j < questionLine.getChildCount(); j++) {
                            // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                            if ("0".equals(questionLine.getChildAt(j).getTag(R.id.selected) + "")) {
                                questionLine.getChildAt(j).setBackgroundColor(Color.parseColor("#3b7830"));
                                // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                                questionLine.getChildAt(j).setTag(R.id.selected, 2);
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView timu_num;
        public FlowLayout questionLine;
        public FlowLayout answerLine;

        public MyViewHolder(View itemView) {
            super(itemView);
            timu_num = (TextView) itemView.findViewById(R.id.timu_num);
            questionLine = (FlowLayout) itemView.findViewById(R.id.questionLine);
            answerLine = (FlowLayout) itemView.findViewById(R.id.answerLine);
        }
    }
}
